package com.glepoch.springbootsecuritymybatis.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

/**
 * 获取spring的ApplicationContext
 * 获取servlet的上下文ServletContext
 *
 * @author ygl
 * @date
 */
@Component
public class ContextUtils {

    private static ApplicationContext applicationContext;
    private static ServletContext servletContext;
    private static HttpServletRequest request;

    public static HttpServletRequest getRequest() {
        return request;
    }
    @Autowired
    public  void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    @Autowired
    public  ServletContext setServletContext(ServletContext servletContext) {
        return this.servletContext=servletContext;
    }

    @Autowired
    public void setApplicationContext(ApplicationContext applicationContext){
        this.applicationContext=applicationContext;
    }

    //获取servletContext
    public static ServletContext getServletContext() {
        return servletContext;
    }

    //获取applicationContext
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    //通过name获取 Bean.
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);

    }

    //通过class获取Bean.
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    //通过name,以及Clazz返回指定的Bean
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }

}